import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/select_location_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/automounting_provider.dart';
import 'package:xiongan_app_yy/widgets/comm/dropdownmenu.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';
import 'package:xiongan_app_yy/widgets/list_widget.dart';

//自动上架
class SelectLocation extends StatefulWidget {
  Map map;
  @override
  State<StatefulWidget> createState() {
    return _SelectLocation();
  }

  SelectLocation(this.map);
}

//ListWidget
class _SelectLocation extends State<SelectLocation> {
  final List<Map<String, dynamic>> _data = List();

  String areaId; //库区id
  String shelvesId; //货架id
  String locationId; //货位id

  String areaVal; //库区
  String shelvesVal; //货架
  String locationVal; //货位

  List areaList=[]; //库区list

  int n_index=0;
  int m_index=0;

  bool load=true;

  //匹配货架
  void setToLocationData(){
    int n=0,m=0;
    List datas1=areaList;
    for(int i=0;i<datas1.length;i++){
      if(datas1[i]['id'] == areaId){
        n=i;
        break;
      }
    }
    List datas2=datas1[n]['shelfList'];
    for(int j=0;j<datas2.length;j++){
      if(shelvesId == datas2[j]['id']){
        m=j;
        break;
      }
    }
    setState((){
      n_index=n;
      m_index=m;
    });
  }

  @override
  void initState() {//初始化库区/货架/货位
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "选择货位",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body:LoadBuilder(visibility: load,builder:  Column(
          children: <Widget>[
            Expanded(
                child: Container(
                  padding: EdgeInsets.only(top: 20, left: 12, right: 12),
                  child: Column(
                    children: <Widget>[
                      DropDownMenu(
                        datas: areaList,
                        keyItem: {"id":"text"},
                        hitText: "选择上架库区",
                        value: areaId,
                        onChange: (obj){
                          setState(() {
                            areaId=obj['value'];
                            shelvesId=null;
                            locationId=null;
                          });
                          setToLocationData();
                          areaVal=obj['item']['text'];
                        },
                      ),
                      DropDownMenu(
                        datas: areaList.isEmpty?[]:areaList[n_index]['shelfList'],
                        keyItem: {"id":"text"},
                        hitText: "选择上架货架",
                        value: shelvesId,
                        onChange: (obj){
                          setState(() {
                            shelvesId=obj['value'];
                            locationId=null;
                          });
                          setToLocationData();
                          shelvesVal=obj['item']['text'];
                        },
                      ),
                      DropDownMenu(
                        datas: areaList.isEmpty?[]:areaList[n_index]['shelfList'][m_index]['locationList'],
                        keyItem: {"id":"value"},
                        hitText: "选择上架货位",
                        value: locationId,
                        onChange: (obj){
                          setState(() {
                            locationId=obj['value'];
                          });
                          locationVal=obj['item']['value'];
                          setToLocationData();
                        },
                      ),
                    ],
                  ),
                )
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: FlatButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(0))),
                    padding: EdgeInsets.all(15),
                    color: Color(0xFF42AFF0),
                    child: Text(
                      "设置",
                      style: TextStyle(color: Colors.white),
                    ),
                    onPressed: () {
                      print("测试");
                      print(areaVal);
                      print(shelvesVal);
                      print(locationVal);
                      widget.map['areaid']=areaId;
                      widget.map['areacode']=areaVal;
                      
                      widget.map['shelfid']=shelvesId;
                      widget.map['shelfcode']=shelvesVal;
                      
                      widget.map['tolocationid']=locationId;
                      widget.map['tolocationcode']=locationVal;
                      Navigator.pop(context);
                    },
                  ),
                )
              ],
            )
          ],
        ))
    );
  }

  void getData() async{
    InternetAPI.getInstance()
        .get("WMS/MiWarehouseInOrder/ArtificialAllotLocation", (data) {
       SelectLocationEntity bean=SelectLocationEntity.fromJson(data);
             setState(() {
               areaList= bean.toJson()["datas"];
               load=false;
               areaId=areaList[0]["id"];
               shelvesId=areaList[0]['shelfList'][0]["id"];
               locationId=areaList[0]['shelfList'][0]['locationList'][0]["id"];
               areaVal=areaList[0]["text"];
               shelvesVal=areaList[0]['shelfList'][0]["text"];
               locationVal=areaList[0]['shelfList'][0]['locationList'][0]["value"];
             });
    }, params: {
      "orderid":widget.map['orderid'],
      "materialid":widget.map['materialid'],
      "orderdispatchdetailid":widget.map['orderdispatchdetailid'],
      "orderdetailid":widget.map['orderdetailid'],
      "warehouseid": DataSingleton().warehouseChoice.id
    },
    );

  }
}
